//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>

#include "libraries.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE 5000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool SplitColumn(char* buffer, TListE<String>* columns, const char* characters) {

  if (!buffer || !columns || !characters) return false;

  char *part = strtok(buffer, characters);

  while (part) {
    String* cell = new String(part);
    cell->Trim();
    columns->Add(cell);
    part = strtok(NULL, characters);
  }

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main(int argc, char** argv) {

  String* filenameIn;
  String* filenameOut;
  String* filenameIOut;

  if (argc < 4) {
    cout << "Error in the input arguments. The execution must be: " << endl;
    cout << "./SMIChangeIDEToNumber <Input SMI file> <Output SMI file> <Output Inter SMI file>" << endl;
    return 0;
  }
  else {
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "   Input SMI File : " << argv[1] << endl;
    cout << "   Ouput SMI File : " << argv[2] << endl;
    cout << " Ouput I SMI File : " << argv[3] << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    filenameIn    = new String(argv[1]);
    filenameOut   = new String(argv[2]);
    filenameIOut  = new String(argv[3]);
  }

  fstream fileIn, fileOut, fileIOut;

  fileIn.open(filenameIn->Out(),     fstream::in);
  fileOut.open(filenameOut->Out(),   fstream::out);
  fileIOut.open(filenameIOut->Out(), fstream::out);

  char             buffer[LENGTHLINE + 1];
  TListE <String>* list    = new TListE <String> ();
  int              counter = 0;

  while (!fileIn.eof()) {

    fileIn.getline(buffer, LENGTHLINE);
    if (strlen(buffer) < 2) continue;
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "Analizing: " << buffer << endl;
    list->Clear();
    SplitColumn(buffer, list, " \n\r");

    if (list->Length()) {
      fileOut  << list->Get(0)->Out() << " " << ++counter << endl;
      fileIOut << list->Get(0)->Out() << " " << list->Get(1)->Out() << " " << counter << endl;
      cout     << "Result: " << list->Get(0)->Out() << " " << counter << endl;
    }

  }

  fileIn.close();
  fileOut.close();
  fileIOut.close();

  if (list)           delete list;

  if (filenameIn)     delete filenameIn;
  if (filenameOut)    delete filenameOut;
  if (filenameIOut)   delete filenameIOut;

  return 1;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//



